function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json);
            document.querySelector("#list").innerHTML = "";
            for (var i=json.data.length-1;i>=0;i--){
                var li = document.createElement("li");
                var spanName = document.createElement("span");
                spanName.innerHTML = json.data[i].studentName;
                var spansex = document.createElement("span");
                spansex.innerHTML = json.data[i].sex;
                var spanAge = document.createElement("span");
                spanAge.innerHTML = json.data[i].age;
                var button = document.createElement('button');

                var button_text = document.createTextNode('X');
                button.addEventListener('click',function () {
                    var nr = document.querySelector('#list');
                    nr.removeChild(this.parentNode);
                });
                button.appendChild(button_text);



                li.appendChild(spanName);
                li.appendChild(spansex);
                li.appendChild(spanAge);
                li.appendChild(button);
                document.querySelector("#list").appendChild(li)
            }
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
    xmlhttp.send();
}
loadXMLDoc();

document.querySelector("#btn").addEventListener('click',function () {
        var n = document.querySelector("#n").value;
        console.log(n);
        var s = document.querySelector("#s").value;
        console.log(s);
        var a = document.querySelector("#a").value;
        console.log(a);
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {

            loadXMLDoc()
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addstudent?studentName="+n+"&sex="+s+"&age="+a,true);
    xmlhttp.send();
});


